<!doctype html>
<html class="no-js">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="viewport"
          content="width=device-width, initial-scale=1">
    <title>二维码</title>

    <!-- Set render engine for 360 browser -->
    <meta name="renderer" content="webkit">

    <!-- No Baidu Siteapp-->
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    {load href="//cdn.bootcss.com/amazeui/2.7.2/css/amazeui.min.css"}
    {load href="//cdn.bootcss.com/jquery/2.1.4/jquery.min.js"}

    {load href="//cdn.bootcss.com/amazeui/2.7.2/js/amazeui.min.js"}
    {load href="//cdn.bootcss.com/layer/3.1.0/mobile/layer.js"}
</head>
<body>
<header class="am-topbar am-container">
    <h1 class="am-topbar-brand">
        <a href="#">二维码扫码登录</a>
    </h1>

    <button class="am-topbar-btn am-topbar-toggle am-btn am-btn-sm am-btn-success am-show-sm-only" data-am-collapse="{target: '#doc-topbar-collapse'}"><span class="am-sr-only">导航切换</span> <span class="am-icon-bars"></span></button>

    <div class="am-collapse am-topbar-collapse" id="doc-topbar-collapse">


        <div class="am-topbar-right"></div>

    </div>
</header>

<div class="am-container">

    <section class="am-panel am-panel-primary">
        <header class="am-panel-hd">
            <h3 class="am-panel-title" id="status">请用微信扫码登录</h3>
        </header>
        <div class="am-panel-bd">
            <div id="doc-qrcode" class="am-text-center"></div>
        </div>

    </section>


</div>

<script>

    var mt_id={$mt_id};
    var ws = new WebSocket('ws://{:env(\'ROOT_URL\')}:8282');
    var bind_url = "{:url('index/index/register')}";
    var check_url = "{:url('index/index/check')}";
    $(function() {
        $qr = $('#doc-qrcode');
        makeCode("{:url('index/index/login_success',['mt_id'=>$mt_id],true,true)}");
        function makeCode(text) {
            $qr.empty().qrcode(text);
        }
    });
    ws.onmessage = function (e) {
        var data = eval("(" + e.data + ")")
        console.log(data);
        if (!data) {
            return;
        }
        switch (data.type) {
            case 'init':
                chat_post({client_id: data.id, mt_id: mt_id},bind_url);
                break;
            case 'success':
                // $("#success_div").fadeIn();
                layer.open({
                    content: '登录成功'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                $('#status').text('登录成功')
                chat_post({mt_id: mt_id},check_url);
                break;
        }

        function chat_post(data,url) {
            $.ajax({
                url: url,
                type: 'POST',
                dataType: 'JSON',
                data: data,
                success: function (data) {
                    console.log(data)
                },
                error:function (e) {
                    console.log('error');
                }
            });
        }
    }
</script>
</body>
</html>

